package sz.szsmk.citizencard.mvp.presenter;

import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import sz.szsmk.citizencard.DaggerApplication;
import sz.szsmk.citizencard.log.L;
import sz.szsmk.citizencard.model.network.GsdxNetApi;
import sz.szsmk.citizencard.model.network.bean.request.ServiceSearchReq;
import sz.szsmk.citizencard.model.network.bean.response.PrepayResponse;
import sz.szsmk.citizencard.model.network.bean.response.ShopListRes;
import sz.szsmk.citizencard.mvp.contract.INetSerchContact;
import sz.szsmk.citizencard.retrofitrxjava.ErrorAction;
import sz.szsmk.citizencard.retrofitrxjava.NetworkDateSources;
import sz.szsmk.citizencard.retrofitrxjava.SubscribeResult;
import sz.szsmk.citizencard.ui.activity.IMainActivity;
import sz.szsmk.citizencard.util.Constant;
import sz.szsmk.citizencard.util.PubUtils;

/**
 * Copyright (c)2018
 * author: zdh
 * created on: 2018/9/9 22:54
 * description:
 */
public class NetServicePresenter extends IBasePresenter
        implements INetSerchContact.INetSerchPresenter<INetSerchContact.INetSerchView> {


    private NetworkDateSources mNetworkDateSource;
    GsdxNetApi gsdxNetApi;

    @Inject
    public NetServicePresenter(GsdxNetApi gsdxNetApi) {
        this.gsdxNetApi = gsdxNetApi;

        this.mNetworkDateSource = NetworkDateSources.getInstance();
    }

    @Override
    public void shopSearch(String word, String latitude, String longitude, int pager) {
        ServiceSearchReq serviceSearchReq = new ServiceSearchReq();
        serviceSearchReq.setKeyWords(word);
        serviceSearchReq.setLatitude(latitude);
        serviceSearchReq.setLongitude(longitude);
        serviceSearchReq.setPage_num(pager + "");
        serviceSearchReq.setPage_size("15");
        String json = new Gson().toJson(serviceSearchReq);
        L.e(Constant.TAG, "shopSearch---->" + json);
        mNetworkDateSource.shopSearch(serviceSearchReq, new SubscribeResult<ShopListRes>() {
            @Override
            protected void onOk(ShopListRes response) {
                List<ShopListRes.ShopListInfosBean> shopListInfos = response.getShopListInfos();
                if (shopListInfos != null) {
                    ((INetSerchContact.INetSerchView) mView).shopSearchSucc(shopListInfos);
                } else {
                    ((INetSerchContact.INetSerchView) mView).shopSearchFail();
                }
            }

            @Override
            protected void onFailure(ShopListRes response) {
                super.onFailure(response);
                ((INetSerchContact.INetSerchView) mView).shopSearchFail();
                PubUtils.popTipOrWarn(DaggerApplication.getIns(), response.getMessage());
            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {
                ((INetSerchContact.INetSerchView) mView).shopSearchFail();
                PubUtils.popTipOrWarn(DaggerApplication.getIns(), throwable.getMessage());

            }
        });
    }

    @Override
    public void sernetSeatch(String word, int currentPager) {
        ServiceSearchReq serviceSearchReq = new ServiceSearchReq();
        serviceSearchReq.setKeyWords(word);
        serviceSearchReq.setPage_num(currentPager + "");
        serviceSearchReq.setPage_size("15");
        String toJson = new Gson().toJson(serviceSearchReq);
        L.e(Constant.TAG, "serviceSearch" + toJson);
        mNetworkDateSource.serviceSearch(serviceSearchReq, new SubscribeResult<PrepayResponse>() {
            @Override
            protected void onOk(PrepayResponse response) {
                String json = new Gson().toJson(response);
                L.e(Constant.TAG, "adsfasdfasd" + json);

                List<PrepayResponse.WebsiteListInfoBean> websiteListInfo = response.getWebsiteListInfo();
                List<ShopListRes.ShopListInfosBean> shopListInfosBeans = new ArrayList<>();
                if (websiteListInfo != null) {
                    for (int i = 0; i <websiteListInfo.size(); i++) {
                        PrepayResponse.WebsiteListInfoBean websiteListInfoBean = websiteListInfo.get(i);

                        String s = new Gson().toJson(websiteListInfoBean);
                        ShopListRes.ShopListInfosBean shopListRes = new Gson().fromJson(s, ShopListRes.ShopListInfosBean.class);
                        shopListInfosBeans.add(shopListRes);
                    }
                }

                if (shopListInfosBeans != null) {
                    ((INetSerchContact.INetSerchView) mView).shopSearchSucc(shopListInfosBeans);
                } else {
                    ((INetSerchContact.INetSerchView) mView).shopSearchFail();
                }
            }

            @Override
            protected void onFailure(PrepayResponse response) {
                super.onFailure(response);
                ((INetSerchContact.INetSerchView) mView).shopSearchFail();
                PubUtils.popTipOrWarn(DaggerApplication.getIns(), response.getMessage());
            }

        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {
                ((INetSerchContact.INetSerchView) mView).shopSearchFail();
            }
        });
    }

}
